package com.batata.continuing.service.tree;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.batata.continuing.dao.tree.TreeDao;
import com.batata.continuing.pojo.base.ResultData;
import com.batata.continuing.pojo.tree.TreeInfo;
import com.batata.continuing.pojo.tree.TreeInfoAll;
import com.batata.continuing.utils.UuidUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static com.batata.continuing.service.common.ResultCommon.getObjectResultData;

@Service
@Transactional
public class TreeService extends ServiceImpl<TreeDao, TreeInfo> {


    /**
     * 创建节点
     *
     * @param treeInfo
     * @return
     */
    public ResultData<Object> createTree(TreeInfo treeInfo) {
        int insertCount = 0;
        try {
            treeInfo.setTreeState(1);
            treeInfo.setTreeId(UuidUtils.getUuidAndRmWire());
            insertCount = this.baseMapper.insert(treeInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getObjectResultData(new ResultData<>(), insertCount);
    }


    /**
     * 查询所有的节点
     *
     * @return
     */
    public TreeInfoAll getAllTree() {
        return getTreeAndChildByTreeId("8f1687e1e11c4822a26aff2caad29aaa");
    }

    public List<TreeInfoAll> getAllTreeNew() {
        List<TreeInfo> treeInfoList = this.baseMapper.selectList(Wrappers.lambdaQuery(TreeInfo.class));
        List<TreeInfoAll> treeInfoAlls = BeanUtil.copyToList(treeInfoList, TreeInfoAll.class);
        List<TreeInfoAll> finalCriterionVos = treeInfoAlls;
        treeInfoAlls = treeInfoAlls.stream()
                                   .filter(treeInfoAll -> "8f1687e1e11c4822a26aff2caad29aaa"
                                           .equals(treeInfoAll.getTreeId()))
                                   .peek(treeInfoAll ->
                                           treeInfoAll.setChildNode(
                                                   getChildNode(treeInfoAll, finalCriterionVos)
                                           )
                                   )
                                   // 排序，如果排序字段为空则排在上面
                                   .sorted(
                                           Comparator.comparing(
                                                   TreeInfoAll::getThreeValue,
                                                   Comparator.nullsFirst(Comparator.naturalOrder())
                                           )
                                   )
                                   .collect(Collectors.toList());
        return treeInfoAlls;
    }

    public List<TreeInfoAll> getChildNode(TreeInfoAll treeInfoAll, List<TreeInfoAll> lsit) {
        return lsit.stream()
                   .filter(
                           now -> ObjectUtil.equals(now.getTreePareatId(), treeInfoAll.getTreeId())
                   )
                   .peek(e -> e.setChildNode(getChildNode(e, lsit)))
                   .sorted(
                           Comparator.comparing(
                                   TreeInfoAll::getThreeValue,
                                   Comparator.nullsFirst(Comparator.naturalOrder())
                           )
                   )
                   .collect(Collectors.toList());
    }


    /**
     * 根据父节点id查询所有的子节点(不包括孙节点)
     *
     * @param pareatId 父节点id
     * @return
     */
    public List<TreeInfo> getChildNodeByPareatId(String pareatId) {

        QueryWrapper<TreeInfo> param = new QueryWrapper<>();
        param.eq("tree_pareat_id", pareatId);
        param.orderByDesc("three_value");

        return this.baseMapper.selectList(param);
    }


    /**
     * 根据treeid查询节点详细信息
     *
     * @param treeId 节点id
     * @return
     */
    public TreeInfo getNodeByTreeId(String treeId) {
        QueryWrapper<TreeInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tree_id", treeId);
        return this.baseMapper.selectOne(queryWrapper);
    }


    /**
     * 根据id查询tree并包括子节点
     *
     * @param treeId 树id
     * @return
     */
    public TreeInfoAll getTreeAndChildByTreeId(String treeId) {
        TreeInfoAll treeInfoAll = this.baseMapper.getTreeAllById(treeId);
        // 判断是否有子节点
        List<TreeInfoAll> childByTreePareatId = this.baseMapper.getChildByTreePareatId(treeInfoAll.getTreeId());
        for (TreeInfoAll info : childByTreePareatId) {
            int childCountByTreePareatId = this.baseMapper.getChildCountByTreePareatId(info.getTreeId());
            if (childCountByTreePareatId > 0) {
                info.setChildNode(getTreeAndChildBytreePareatId(info.getTreeId()));
            }
        }
        treeInfoAll.setChildNode(childByTreePareatId);
        return treeInfoAll;
    }

    /**
     * 根据父节点id查询所有的子节点
     *
     * @param treePareatId 父节点id
     * @return
     */
    public List<TreeInfoAll> getTreeAndChildBytreePareatId(String treePareatId) {
        List<TreeInfoAll> childByTreePareatId = this.baseMapper.getChildByTreePareatId(treePareatId);
        for (TreeInfoAll treeInfo : childByTreePareatId) {
            int childCountByTreePareatId = this.baseMapper.getChildCountByTreePareatId(treeInfo.getTreeId());
            if (childCountByTreePareatId > 0) {
                treeInfo.setChildNode(getTreeAndChildBytreePareatId(treeInfo.getTreeId()));
            }
        }
        return childByTreePareatId;
    }


}
